package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Override
    public R<Employee> login(Employee employee) {
        Employee dbemployee = employeeMapper.login(employee);
        if (dbemployee == null){
            return R.error("用户名不存在");
        }
        if (!dbemployee.getPassword().equals(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()))){
            return R.error("密码错误");
        }
        if (dbemployee.getStatus() == 0){
            return R.error("该用户已被禁用");
        }
        return R.success(dbemployee);
    }

    @Override
    public void save(Employee employee) {
        employee.setStatus(1);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeMapper.save(employee);
    }

    @Override
    public R<Page> findByPage(int pageNum, int pageSize, String name) {
        //设置页面大小和页面数据
        PageHelper.startPage(pageNum, pageSize, name);
        //显示数据
        List<Employee> employeeList = employeeMapper.findByName(name);
        //创建PageInfo
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);

        //创建Page对象
        Page<Employee> page = new Page<>(pageInfo.getList(),pageInfo.getTotal(),pageSize,pageNum);
        return R.success(page);
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    @Override
    public R<Employee> findById(Long id) {
        Employee employee = employeeMapper.findById(id);
        return R.success(employee);
    }
//    @Autowired(required = false)
//    private EmployeeMapper employeeMapper;
//
//    @Override
//    public R<Employee> login(Employee employee) {
//        Employee dbemployee = employeeMapper.login(employee);
//
//        if (dbemployee == null){
//            return R.error("用户名不存在");
//        }
//        if (!dbemployee.getPassword().equals(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()))){
//            return R.error("密码错误");
//        }
//        if (dbemployee.getStatus() == 0){
//            return R.error("该用户已被禁用");
//        }
//        return R.success(dbemployee);
//    }
//
//    @Override
//    public void save(Employee employee) {
//        employee.setStatus(1);
//        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employeeMapper.save(employee);
//    }
//
//    @Override
//    public R<Page> findByPage(int pageNum, int pageSize, String name) {
//        //设置开始页与页面大小
//        PageHelper.startPage(pageNum,pageSize);
//        //查询页面数据
//        List<Employee> employeelist =  employeeMapper.findByName(name);
//        //创建PageInfo对象
//        PageInfo<Employee> pageInfo = new PageInfo<>(employeelist);
//
//        //转换为Page对象
//        Page<Employee> page = new Page<>(pageInfo.getList(),pageInfo.getTotal(),pageSize,pageNum);
//        return R.success(page);
//
//    }
//
//    @Override
//    public void updateById(Employee employee) {
//        employeeMapper.updateById(employee);
//    }
//
//    @Override
//    public R<Employee> findById(Long id) {
//       Employee employee = employeeMapper.findById(id);
//        return R.success(employee);
//    }
}
